Imports System.Data.Common
Partial Class UserControls_EasyPage
    Inherits System.Web.UI.UserControl

    Public Sql As String
    Public Params() As GenParameter
    Public CountSql As String
    Public ShowButton As Boolean
    Public Size As Integer = 30
    Public CommandButton As Boolean = False
    Public CommandButtonText As String = "View"
    Public ShowImmediately As Boolean = False
    Public Event ButtonPress(ByVal SelectedRow As GridViewRow)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Hide the paging Controls
        PageBehind.Visible = False
        PageAhead.Visible = False
        dlPageList.Visible = False
        MakePageChoice.Visible = False

        'Put a command button if necessary

        If CommandButton = True And Not Page.IsPostBack Then
            Dim bField As New ButtonField
            bField.ButtonType = ButtonType.Link
            bField.Text = CommandButtonText
            Activities.Columns.Add(bField)
        End If

        If Not Page.IsPostBack Then

            'Determine the behavior of the grid
            If ShowButton = False Then
                Button1.Visible = False
                'Call the PushButton
                SubmitParams()
            Else
                Button1.Visible = True
            End If

            'Show immediately if neccesary

            If ShowImmediately = True And ShowButton = True Then
                SubmitParams()
            End If



        End If

    End Sub


    Protected Sub ShowHide()

        Dim NumPages As Integer

        NumPages = HoldNumPages.Value

        'Show the Controls

        PageBehind.Visible = True
        PageAhead.Visible = True
        dlPageList.Visible = True
        MakePageChoice.Visible = True

        'Hide or show the previous and next controls

        If HoldCurrentPage.Value = "1" Then
            PageBehind.Visible = False
        Else
            PageBehind.Visible = True
        End If

        If HoldCurrentPage.Value = NumPages Then
            PageAhead.Visible = False
        Else
            PageAhead.Visible = True
        End If
    End Sub

    Protected Sub UpdateControls(ByVal CurrentPage As Integer)

        'Update the current page
        HoldCurrentPage.Value = CurrentPage
        'Update the dropdownbox selection
        dlPageList.SelectedValue = CurrentPage

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        SubmitParams()
    End Sub

    Protected Sub MakePageChoice_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MakePageChoice.Click
        Dim CurrentPage As Integer

        CurrentPage = CInt(dlPageList.Text)
        PopulateGrid(CurrentPage)

        'Update the controls

        UpdateControls(CurrentPage)

        ShowHide()
    End Sub

    Protected Sub PageAhead_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles PageAhead.Click
        Dim CurrentPage As Integer

        CurrentPage = HoldCurrentPage.Value + 1

        PopulateGrid(CurrentPage)


        'Update the controls

        UpdateControls(CurrentPage)

        ShowHide()
    End Sub

    Protected Sub PageBehind_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles PageBehind.Click
        Dim CurrentPage As Integer

        CurrentPage = HoldCurrentPage.Value - 1
        PopulateGrid(CurrentPage)

        'Update the controls

        UpdateControls(CurrentPage)

        ShowHide()
    End Sub

    Public Sub HideButton()
        Button1.Visible = False
    End Sub

    Protected Sub SubmitParams()

        Dim Count As Integer
        Dim NumRecords As Integer

        'Check for validation
        If Page.IsPostBack Then

            If Not Page.IsValid Then
                Return
            End If

        End If
        

        'Count the number of records
        NumRecords = GenHelper.ExecuteScalar(CountSql, Params, ConfigurationManager.ConnectionStrings.Item("MySqlConnString").ToString())

        PopulateGrid(1)


        'Populate the hidden fields and the dropdownbox

        'Get the Number of Pages

        If NumRecords Mod 30 = 0 Then
            HoldNumPages.Value = CInt(NumRecords / Size)
        Else
            If NumRecords Mod 30 > (Size / 2) Then
                HoldNumPages.Value = CInt(NumRecords / Size)
            Else
                HoldNumPages.Value = CInt(NumRecords / Size) + 1
            End If

        End If

        'The number of pages is always atleast one

        If HoldNumPages.Value = 0 Then
            HoldNumPages.Value = 1
        End If

        dlPageList.Items.Clear()

        For Count = 1 To HoldNumPages.Value
            dlPageList.Items.Add(Count)
        Next
        If Count = 1 Then
            dlPageList.Items.Add(1)
        End If
        'Make the selection in the drop downbox
        UpdateControls(1)

        ShowHide()

    End Sub

    Protected Sub PopulateGrid(ByVal CurrentPage As Integer)

        Dim Limits As String

        Dim Records As DbDataReader
        Dim ThisSql As String

        ThisSql = Sql
        Limits = " LIMIT " & (CurrentPage - 1) * Size & ", " & Size
        ThisSql = ThisSql & Limits
        Records = GenHelper.ExecuteReader(ThisSql, Params, ConfigurationManager.ConnectionStrings.Item("MySqlConnString").ToString())
        Activities.DataSource = Records
        Activities.DataBind()
        Records.Close()

    End Sub

    Protected Sub Activities_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles Activities.RowCommand

        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)


        RaiseEvent ButtonPress(Activities.Rows(Index))

    End Sub
End Class
